import axiosInstance from '@/utils/request'

export const loginAPI = (data) => {
    return axiosInstance.post('/user/auth/login', data)
}

export const UserRegister = (data) => {
    return axiosInstance.post('/user/auth/register', data,
        {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        }
    )
}

export const updateAvatar = (data) => {
    return axiosInstance.post('/user/auth/updateAvatar', data)
}

/**
 * 更新用户信息
 * @param {number} userId - 要更新的用户ID
 * @param {Object} userDTO - 更新的用户数据，可以是 FormData
 * @returns {Promise} Axios Promise
 */
export const updateUser = (userId, userDTO) => {
    // 检查 userDTO 是否是 FormData 对象
    const headers = userDTO instanceof FormData ? { 'Content-Type': 'multipart/form-data' } : {};

    // 添加 Authorization 头部
    const token = localStorage.getItem('token');
    if (token) {
        headers.Authorization = `Bearer ${token}`;
    }

    return axiosInstance.put(`/user/users/${userId}`, userDTO, { headers });
}

/**
 * 更新用户密码
 * @param {*} data
 * @returns
 */
export const updatePassword = (data) => {
    return axiosInstance.put(`/user/users/${data.userId}/password`, {
        oldPassword: data.oldPassword,
        newPassword: data.newPassword
    }, {
        headers: {
            Authorization: `Bearer ${localStorage.getItem('token')}`
        }
    });
}